var _datasDatagrid,selectForm,createDate1,createDate2;
var dialogWeixinMsg;

function deleteRow() {
	var rows = _datasDatagrid.datagrid('getSelections');
	if (rows.length > 0) {
		$.messager.confirm('请确认', '您确实要删除当前所有选择的记录吗？', function(b){
			if (b) {
				var ids = [];
				for ( var i = 0; i < rows.length; i++) {
					ids.push(rows[i].id);
				}
				$.post(sy.bp()+"/admin/weixin/receiveMsg/deletes", {ids:ids.join(",")}, function(data) {
					if (data.result==1) {
						$.messager.show({msg:data.msg,title:'系统提示'});
						_datasDatagrid.datagrid('unselectAll');
						_datasDatagrid.datagrid('reload');
					} else {
						$.messager.alert('警告', data.msg, 'warning');
					}
				}, "json");
			}
		});
	} else {
		$.messager.alert('提示', '请选择要删除的记录', 'error');
	}
}

function replyRow() {
	$.messager.alert('提示', '您好,微信平台暂时没有开放快捷回复接口,请登陆微信平台快捷回复消息!', 'error');
}

function templateRow() {
	var rows = _datasDatagrid.datagrid('getSelections');
	if (rows.length == 1) {
		$.post(sy.bp()+"/admin/weixin/templateMsg/send", {touser:rows[0].fromUserName}, function(data) {
			if (data.result==1) {
				$.messager.show({msg:data.msg,title:'系统提示'});
				_datasDatagrid.datagrid('unselectAll');
				_datasDatagrid.datagrid('reload');
			} else {
				$.messager.alert('警告', data.msg, 'warning');
			}
		}, "json");
	} else {
		$.messager.alert('提示', '只能选择一条记录!', 'error');
	}
}

function search_datas() {
	var formData = selectForm.form('getData', true);
	if(formData.createDate1 != '' && formData.createDate2 != '' && formData.createDate1 > formData.createDate2){
		$.messager.alert('提示', '开始时间不得大于结束时间，请重新选择！', 'warning');
	} else {
		if (selectForm.form('validate')) {
			_datasDatagrid.datagrid('load',sy.serializeObject(selectForm));
		}
	}
};

$(function(){
	_datasDatagrid = $('#_datasDatagrid');
	selectForm = $('#selectForm');
	var jsonArr = [
       {value:'text',text:'文本类型'},{value:'image',text:'图片类型'},
       {value:'voice',text:'语音类型'},{value:'video',text:'视频类型'},
	   {value:'music',text:'音乐类型'},{value:'news',text:'图文类型'}
	];
	dialogWeixinMsg = $('#dialogWeixinMsg').show().dialog({
		resizable : false,
		closed : true,
		closable : true,
		modal : true,
	});
	_datasDatagrid.datagrid({
		url : sy.bp()+'/admin/weixin/receiveMsg/list',
		fit : true,
		autoRowHeight : false,
		striped : true,
		rownumbers : true,
		pagination : true,
		singleSelect : false,
		fitColumns : true,
		pageSize : 10,
		pageList : [10,20,30,40,50],
		nowrap : true,
		border : false,
		idField : 'id',
		sortName : 'createTime',
		sortOrder : 'desc',
		view: sy.myview,
        emptyMsg: '没有查询到相关数据',
		toolbar: [
			{text:'批量删除',iconCls:'icon-remove',handler: deleteRow},'-',
			{text:'批量回复',iconCls:'icon-feed',handler: replyRow},'-',
			{text:'模版消息',iconCls:'icon-feed',handler: templateRow},'-',
			{text:'数据刷新',iconCls:'icon-reload',handler: function(){_datasDatagrid.datagrid('reload');}}
		],
		frozenColumns: [[
            {field:'id',title:'编号',align:'center',width:100,sortable:true,checkbox:true},
		]],
		columns:[[
		    {field:'nickname',title:'用户',align:'left',width:100,sortable:true},
		    {field:'sex',title:'性别',align:'center',width:100,sortable:true},
		    {field:'city',title:'城市',align:'center',width:100,sortable:true},
		    {field:'subscribeDate',title:'关注时间',align:'center',width:200,sortable:true},
		    {field:'remark',title:'备注',align:'left',width:100,sortable:true},
		    {field:'createTime',title:'CreateTime',align:'left',width:100,sortable:true,hidden:true},
		    {field:'createDate',title:'createDate',align:'center',width:200,sortable:true},
          	{field:'msgType',title:'消息类型',align:'center',width:100,sortable:true,
            	formatter:function(value, row) {
            		for ( var i = 0; i < jsonArr.length; i++) {
						if (value == jsonArr[i].value) {
							return jsonArr[i].text;
						}
					}
            	}
	        },
          	{field:'content',title:'文本内容',align:'left',width:300,sortable:true}
		]],
		onLoadSuccess : function(data) {
			$.messager.progress('close');
		},
		onDblClickRow : function(rowIndex, rowData) {
		}
	});
});